package com.itheima.controller;

import com.itheima.domain.KsClass;
import com.itheima.domain.User;
import com.itheima.service.LoginService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v1/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @GetMapping("/{mobile}")
    public Result<List<KsClass>> selectAndJustRole(@PathVariable("mobile") String mobile) {
        List<KsClass> ksClassList = loginService.selectAndJustRole(mobile);
        if (ksClassList == null) {
            return new Result<>(1, "操作失败", null);
        } else {
            return new Result<>(0, "操作成功", ksClassList);
        }
    }

    @PostMapping("/in")
    public Result login(@RequestBody User user, HttpSession session) {
        session.setAttribute("classId", user.getClassId());
        if (loginService.login(user)) {
            return new Result<>(0, "登录成功", user);
        } else {
            return new Result<>(1, "登陆失败", null);
        }
    }
}
